import { CANVAS_WIDTH, CANVAS_HEIGHT } from '../utils/constants.js'

// 敌机基类
export default class Enemy {
  constructor(width, height, speed, health, score, type, imageSrc) {
    this.width = width
    this.height = height
    this.speed = speed * (1 + Math.random() * 0.3)
    this.health = health
    this.score = score
    this.type = type
    this.x = Math.random() * (CANVAS_WIDTH - width)
    this.y = -height

    // 加载图像
    this.image = new Image()
    this.image.src = imageSrc
  }

  update() {
    this.y += this.speed
  }

  render(ctx) {
    ctx.drawImage(this.image, this.x, this.y, this.width, this.height)

    // 绘制生命值条（对于中型和大型敌机）
    if (this.type !== 'small') {
      this.renderHealthBar(ctx)
    }
  }

  renderHealthBar(ctx) {
    const healthBarWidth = this.width * 0.8
    const healthBarHeight = 4
    const healthBarX = this.x + (this.width - healthBarWidth) / 2
    const healthBarY = this.y - 8

    // 背景
    ctx.fillStyle = '#333'
    ctx.fillRect(healthBarX, healthBarY, healthBarWidth, healthBarHeight)

    // 生命值
    const healthPercent = this.health / this.maxHealth
    ctx.fillStyle = healthPercent > 0.5 ? '#00ff00' : '#ff0000'
    ctx.fillRect(
      healthBarX,
      healthBarY,
      healthBarWidth * healthPercent,
      healthBarHeight
    )
  }

  isOutOfScreen() {
    return this.y > CANVAS_HEIGHT
  }

  takeDamage(amount) {
    this.health -= amount
  }
}
